package com.tiyende.kash.loop.ui.activity

import android.graphics.Color
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.widget.ViewPager2
import com.tiyende.kash.loop.R
import com.tiyende.kash.loop.common.CommonActivity
import com.tiyende.kash.loop.databinding.TiyendeActivityGuideBinding
import com.tiyende.kash.loop.databinding.TiyendeGuideCursorBinding
import com.tiyende.kash.loop.event.TiyendeEventPointType
import com.tiyende.kash.loop.ui.adapter.TiyendeGuidePagerAdapter
import com.tiyende.kash.loop.utils.TiyendeDensityUtil
import com.tiyende.kash.loop.utils.TiyendeTitleBarUtil
import com.tiyende.kash.loop.utils.helper.TiyendeAnalysisHelper
import com.tiyende.kash.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.kash.loop.utils.shared.TiyendeSharedKey
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.json.JSONObject

/**
 * TiyendeCredit
 * @Author wwdeng
 * @CreateTime: 2024/12/9 14:48
 * @Annotation：引导页
 */
class TiyendeGuideActivity: CommonActivity() {
    lateinit var binding: TiyendeActivityGuideBinding
    private var guideSlides: ArrayList<JSONObject>? = null
    private var showPageIndex = 0
    override fun getTiyendeRootView(): View {
        binding = TiyendeActivityGuideBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initTiyendeView() {
        TiyendeTitleBarUtil.setStatusBarLightMode(this, false)
    }

    override fun initTiyendeListener() {
        binding.guideViewPage.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                showPageIndex = position
                lifecycleScope.launch{
                    delay(100)
                    showGuidePageContent()
                }
            }
        })
        binding.btnGuideNext.setOnClickListener {
            //下一步
            onGuideStartNextStep()
        }
    }

    override fun loadTiyendeData() {
        guideSlides = arrayListOf()
        val guide1 = JSONObject()
        guide1.put("title",getString(R.string.tiyende_guide_page_convenient))
        guide1.put("reason",getString(R.string.tiyende_guide_page_convenient_msg))
        guide1.put("icon",R.mipmap.tiyende_guide1)
        guideSlides?.add(guide1)

        val guide2 = JSONObject()
        guide2.put("title",getString(R.string.tiyende_guide_page_fees_transparent))
        guide2.put("reason",getString(R.string.tiyende_guide_page_fees_transparent_msg))
        guide2.put("icon",R.mipmap.tiyende_guide2)
        guideSlides?.add(guide2)

        val guide3 = JSONObject()
        guide3.put("title",getString(R.string.tiyende_guide_page_safe))
        guide3.put("reason",getString(R.string.tiyende_guide_page_safe_msg))
        guide3.put("icon",R.mipmap.tiyende_guide3)
        guideSlides?.add(guide3)

        guideSlides?.let {
            binding.guideViewPage.adapter = TiyendeGuidePagerAdapter(it)
        }
    }

    /**
     * 显示内容
     */
    private fun showGuidePageContent(){
        showGuideCursorLayout()
        guideSlides?.let { list ->
            val item = list[showPageIndex]
            binding.txtGuideType.text = item.optString("title")
            binding.txtGuideMsg.text = item.optString("reason")
        }
    }

    /**
     * 添加指示器
     */
    private fun showGuideCursorLayout(){
        binding.guideCursorLayout.removeAllViews()
        for (i in 0..< 3){
            val indicatorBinding = TiyendeGuideCursorBinding.inflate(layoutInflater)
            val params = indicatorBinding.cursorView.layoutParams
            if (i == showPageIndex){
                indicatorBinding.cursorView.shapeDrawableBuilder.setSolidColor(Color.parseColor("#FF00FFC0")).intoBackground()
                params.width = TiyendeDensityUtil.dp2px(44f)
            } else {
                indicatorBinding.cursorView.shapeDrawableBuilder.setSolidColor(Color.parseColor("#8000FFC0")).intoBackground()
                params.width = TiyendeDensityUtil.dp2px(28f)
            }
            indicatorBinding.cursorView.layoutParams = params
            binding.guideCursorLayout.addView(indicatorBinding.root)
        }
    }

    /**
     * 下一步
     */
    private fun onGuideStartNextStep(){
        TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_SHOW_GUIDE_PAGE,true)
        //已登录 - 进入主页
        if (TiyendeSharedHelper.queryTiyendeSharedBoolean(TiyendeSharedKey.TIYENDE_LOGIN)){
            //初始化analysis lib 赋值 用户id 和 token
            TiyendeAnalysisHelper.initAnalysisLibAtLoginSuccess()
            showLaunchMainPage()
            return
        }
        //进入登录页面
        showLaunchLoginPage()
    }

    /**
     * 进入主页
     */
    private fun showLaunchMainPage(){
        onIntent(TiyendeMainActivity::class.java)
        finish()
    }

    /**
     * 进入登录页
     */
    private fun showLaunchLoginPage() {
        onIntent(TiyendeLoginActivity::class.java)
        finish()
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_GUIDE_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        addEventPointRequestAction(TiyendeEventPointType.PAGE.TIYENDE_PAGE_GUIDE_END)
    }
}